package com.itwk.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itwk.common.utils.PageUtils;
import com.itwk.common.utils.Query;
import com.itwk.gulimall.product.dao.PmsBrandDao;
import com.itwk.gulimall.product.entity.PmsBrandEntity;
import com.itwk.gulimall.product.service.PmsBrandService;
import com.itwk.gulimall.product.service.PmsCategoryBrandRelationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("pmsBrandService")
public class PmsBrandServiceImpl extends ServiceImpl<PmsBrandDao, PmsBrandEntity> implements PmsBrandService {

    //注入品牌分类关联关系service
    @Autowired
    private PmsCategoryBrandRelationService relationService;

    //品牌信息的分页查询，如果带有查询条件需要根据查询条件查询
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        QueryWrapper<PmsBrandEntity> wrapper = new QueryWrapper<>(); //创建查询条件
     //如果封装有查询条件key那么构建查询条件
        if(!StringUtils.isEmpty(key)){
           wrapper.eq("brand_id", key).or().like("name", key).or().like("descript", key);
        }

        IPage<PmsBrandEntity> page = this.page(
                new Query<PmsBrandEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    //修改品牌名时关联修改关联表中数据。
    @Override
    public void updateRelation(PmsBrandEntity pmsBrand) {
        baseMapper.updateById(pmsBrand);
        if(!StringUtils.isEmpty(pmsBrand.getName())){  //品牌名不为空，可能修改了名称，同步修改到关联关系表中
            relationService.updateBrandRelation(pmsBrand.getBrandId(),pmsBrand.getName());
        }
    }

}